//
//  oldHub.m
//  laonianbang
//
//  Created by 勇拓 李 on 17/10/9.
//  Copyright © 2017年 zaiyou. All rights reserved.
//

#import "oldHub.h"
#import "MBProgressHUD.h"
#import "AppDelegate.h"

@implementation oldHub

+ (void) warningUser:(NSString *) msg{
    
    [oldHub hideHUD];
    //    dispatch_sync(dispatch_get_main_queue(), ^{
    //Update UI in UI thread here
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[self getCurrentVC].view animated:YES];
    hud.detailsLabel.text = msg;
    hud.mode = MBProgressHUDModeText;
    [hud hideAnimated:YES afterDelay:0.8];
    //    });
}

+ (void) warningWindow:(NSString *)msg
{
    [oldHub hideHUDWindow];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[AppDelegate getMainWindow] animated:YES];
    hud.detailsLabel.text = msg;
    hud.mode = MBProgressHUDModeText;
    [hud hideAnimated:YES afterDelay:0.8];
    //    });
}

+ (void)warningUser:(NSString *) msg time:(NSInteger)time
{
    [oldHub hideHUD];
    //    dispatch_sync(dispatch_get_main_queue(), ^{
    //Update UI in UI thread here
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[self getCurrentVC].view animated:YES];
    hud.detailsLabel.text = msg;
    hud.mode = MBProgressHUDModeText;
    [hud hideAnimated:YES afterDelay:time];
    //    });
    
}

+ (void) warningUserAfterJump:(NSString *)msg time:(NSInteger)time {
    
    [oldHub hideHUD];
    //    dispatch_sync(dispatch_get_main_queue(), ^{
    //Update UI in UI thread here
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[AppDelegate getMainWindow] animated:YES];
    hud.detailsLabel.text = msg;
    hud.mode = MBProgressHUDModeText;
    [hud hideAnimated:YES afterDelay:time];
    //    });
}


+ (void) waiting:(NSString *)msg {
    //    dispatch_sync(dispatch_get_main_queue(), ^{
    //Update UI in UI thread here
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[self getCurrentVC].view animated:YES];
    if (msg) {
        hud.label.text = msg;
    } else {
        hud.mode = MBProgressHUDModeIndeterminate;
    }
    //    });
}

+ (void) waitingWindow:(NSString *)msg {
    //    dispatch_sync(dispatch_get_main_queue(), ^{
    //Update UI in UI thread here
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[AppDelegate getMainWindow] animated:YES];
    if (msg) {
        hud.label.text = msg;
    } else {
        hud.mode = MBProgressHUDModeIndeterminate;
    }
    //    });
}

+ (void)hideHUD{
    
    for(id subview in [self getCurrentVC].view.subviews)
    {
        if ([subview isKindOfClass:[MBProgressHUD class]]) {
            //            dispatch_sync(dispatch_get_main_queue(), ^{
            //Update UI in UI thread here
            MBProgressHUD *hud = (MBProgressHUD *)subview;
            [hud hideAnimated:YES];
            //            });
        }
    }
}

+ (void)hideHUDWindow{
    
    for(id subview in [AppDelegate getMainWindow].subviews)
    {
        if ([subview isKindOfClass:[MBProgressHUD class]]) {
            //            dispatch_sync(dispatch_get_main_queue(), ^{
            //Update UI in UI thread here
            MBProgressHUD *hud = (MBProgressHUD *)subview;
            [hud hideAnimated:YES];
//            return;
            //            });
        }
    }
}

+ (UIActivityIndicatorView *) showIndicator:(UIViewController *)viewController {
    
    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    UIBarButtonItem* btn_indicator = [[UIBarButtonItem alloc] initWithCustomView:indicator];
    viewController.navigationItem.rightBarButtonItem = btn_indicator;
    [indicator startAnimating];
    
    return indicator;
}

+ (UIActivityIndicatorView *) showIndicatorWithUINavigtationItem:(UINavigationItem *)navigationItem {
    
    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    UIBarButtonItem* btn_indicator = [[UIBarButtonItem alloc] initWithCustomView:indicator];
    navigationItem.rightBarButtonItem = btn_indicator;
    [indicator startAnimating];
    
    return indicator;
}

+ (void) hideIndicator:(UIViewController *)viewController indicator:(UIActivityIndicatorView *)indicator{
    
    if (indicator) {
        [indicator stopAnimating];
        
    }
}

+ (void) hideIndicator:(UIViewController *)viewController indicator:(UIActivityIndicatorView *)indicator insteadOf:(UIBarButtonItem *)btn{
    
    if (indicator) {
        [indicator stopAnimating];
        viewController.navigationItem.rightBarButtonItem = btn;
    }
}


+ (void) hideIndicatorWithUINavigationItem:(UINavigationItem *)navigationItem indicator:(UIActivityIndicatorView *)indicator {
    
    if (indicator) {
        [indicator stopAnimating];
        
    }
}

+ (void) hideIndicatorWithUINavigationItem:(UINavigationItem *)navigationItem indicator:(UIActivityIndicatorView *)indicator insteadOf:(UIBarButtonItem *)btn {
    
    if (indicator) {
        [indicator stopAnimating];
        navigationItem.rightBarButtonItem = btn;
    }
}

+ (void) showAlertView:(UIViewController *)viewController alertMsg:(NSString *)msg {
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"ops" message:msg delegate:nil cancelButtonTitle:@"恩" otherButtonTitles:nil, nil];
    [viewController.view addSubview:alert];
    [alert show];
}

+ (UIViewController *)getCurrentVC{
    
    UIViewController *result = nil;
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    //app默认windowLevel是UIWindowLevelNormal，如果不是，找到UIWindowLevelNormal的
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    id  nextResponder = nil;
    UIViewController *appRootVC=window.rootViewController;
    //    如果是present上来的appRootVC.presentedViewController 不为nil
    if (appRootVC.presentedViewController) {
        nextResponder = appRootVC.presentedViewController;
    }else{
        UIView *frontView = [[window subviews] objectAtIndex:0];
        nextResponder = [frontView nextResponder]; //  这方法下面有详解    </span>
    }
    
    if ([nextResponder isKindOfClass:[UITabBarController class]]){
        UITabBarController * tabbar = (UITabBarController *)nextResponder;
        UINavigationController * nav = (UINavigationController *)tabbar.viewControllers[tabbar.selectedIndex];
        //        UINavigationController * nav = tabbar.selectedViewController ; 上下两种写法都行
        result=nav.childViewControllers.lastObject;
        
    }else if ([nextResponder isKindOfClass:[UINavigationController class]]){
        UIViewController * nav = (UIViewController *)nextResponder;
        result = nav.childViewControllers.lastObject;
    }else{
        result = nextResponder;
    }
    
    return result;
}

@end
